/*
 * jQuery Panel 0.1
 * http://www.bsweb.com.cn
 * 需要库文件
 * 		jquery-ui.js
 * 		buss.toolbar.js
 */
(function($,undefined){
	$.widget("ui.panel",{
		options:{
			title:null,					//Panel标题文本			
			width:'auto',				//Panel宽度
			height:'auto',				//Panel高度			
			iconCls:null,				//Panel图片Class
			headerCls:null,		//Panel的Title的Class
			bodyCls:null,		//Panel内容Class			
			content:false,				    //Panel的内容容器
			header:false,					//是否需要Title栏
			closable:false,					//是否需要Close按钮
			minimizable:false,				//是否需要最下化按钮
			collapsible:false,				//是否需要收缩按钮
			bodyContent:'body',
			isDialog:false,					//是否弹出窗口
			iframe:false,
			topTools:[],					//Panel上面工具栏
			bottomTools:[],					//Panel下面工具栏
			loadUrl:null,					//加载内容到Panel
			laodParams:null,				//加载内容参数
			loadingMessage:'Loading...',	//加载数据未完成时显示的信息
			onLoad:function(){},			//加载完成触发的事件
			onBeforeOpen:function(){},		//在打开Panel之前触发的事件
			onOpen:function(){},			//在打开Panel之后触发的事件
			onBeforeClose:function(){},		//Panel关闭之前触发
			onClose:function(){},			//在关闭Panel之后触发的事件
			onBeforeDestroy:function(){},	//在销毁Panel之前触发的事件
			onDestroy:function(){},			//在销毁Panel之后触发的事件
			onBeforeMinimize:function(){},	//最小化Panel之后触发的事件
			onMinimize:function(){},		//最小化Panel之后触发的事件
			onBeforeCollapse:function(){},	//Panel收缩之前触发的事件
			onCollapse:function(){},		//Panel收缩之后触发的事件
			onBeforeExpand:function(){},		//Panel展开之前触发的事件
			onExpand:function(){},			//Panel展开之后触发的事件
			onBeforeRestore:function(){},	//
			onRestore:function(){}
		},
		/**
		 * 创建Panel所用的模板
		 */
		_create:function(){
			this._createPanel();
		},
		_createPanel:function(){
			var panelHeaderTpl = "<div class='buss-panel-header'><div class='buss-panel-with-icon'>My Panel</div>"+
				      "<div class='buss-panel-icon'/><div class='buss-panel-tool'>"+
					  "<div class='buss-panel-tool-close'/><div class='buss-panel-tool-min'/><div class='buss-panel-tool-collapse'/>" +
					  "</div></div>";//buss-panel-top-toolbar-left
			var topbar = "";
				if(this.options.isDialog){
					topbar = "<table class='buss-panel-toolbar-border' cellspacing='0' cellpadding='0'><tbody><tr><td class='buss-panel-top-toolbar-left'></td><td><div class='buss-panel-top-toolbar'></div></td><td class='buss-panel-top-toolbar-right'></td></tr></table>";				
				}else{
					topbar = "<div class='buss-panel-top-toolbar'></div>";
				}
			var panelBodyTpl = "<div class='buss-panel-body'>" +
					  topbar +  
					  "<div class='buss-panel-body-content'>loading...</div>" +
					  "<div class='buss-panel-bottom-toolbar'></div>"  
					  "</div>";
			this.bodyContent = this.element.html();
			this.element.html("");
			this.panelHeaderTemplate = $(panelHeaderTpl);
			this.panelBodyTemplate = $(panelBodyTpl);
			
			
			this.panelTemplate = this.element.append(this.panelHeaderTemplate).append(this.panelBodyTemplate).addClass("buss-panel");
			if(this.panelTemplate.parent().attr("tagName") == undefined){
				this.panelTemplate.appendTo($("body"));
			}
		},
		/**
		 * 初始化Panel信息
		 */
		_init:function(){
			this._initPanel();
		},		
		_initPanel:function(){
			var opts = this.options;
			var _width = opts.width;
			var _height = opts.height;
			
			if($.isFunction(opts.onBeforeOpen)){
				if(opts.onBeforeOpen.call(this.panelTemplate) == false) return;
			}
			
			this._setHeaderTool();
			
			if (isNaN(_width)){
				_width = 'auto';
			}
			
			var _tmpWidth = this.element.css("width");	
			_tmpWidth = _tmpWidth.substring(0,_tmpWidth.indexOf("px"));
			if(_width == 'auto'){
				_width = _tmpWidth;
			}		

			_width = parseInt(_width);
			if(isNaN(_height)){
				_height = 'auto';
			}
			
			if(this.element.css("height")){
				var _tmpHeight = this.element.css("height");
				_tmpHeight = _tmpHeight.substring(0,_tmpHeight.indexOf("px"));
				if(_height == 'auto'){
					_height = _tmpHeight;
				}
			}
			_height = parseInt(_height);
			
			
			
			this.panelTemplate.css({width:_width,height:_height,display:'block'});
			if(this.options.isDialog){
				//alert(this.panelBodyTemplate.find("buss-panel-body-content").html());
				this.panelBodyTemplate.find(".buss-panel-body-content").addClass("buss-panel-body-border");
				this.panelBodyTemplate.find(".buss-panel-toolbar-border").width(_width - 2);
			}
			
			if($.browser.msie){
				this.panelTemplate.find(".buss-panel-header").width(_width - 12);
			}else{
				this.panelTemplate.find(".buss-panel-header").width(_width - 12);
			}
			
			if(opts.topTools.length < 1){
				this.panelBodyTemplate.find(".buss-panel-top-toolbar").hide();
			}else{
				this.panelBodyTemplate.find(".buss-panel-top-toolbar").show();
				this.panelBodyTemplate.find(".buss-panel-top-toolbar").toolbar({
					items:opts.topTools
				})
			}
			if(opts.bottomTools.length < 1){
				this.panelBodyTemplate.find(".buss-panel-bottom-toolbar").hide();
			}else{
				this.panelBodyTemplate.find(".buss-panel-bottom-toolbar").show();
				this.panelBodyTemplate.find(".buss-panel-bottom-toolbar").toolbar({
					items:opts.bottomTools
				})
			}
			
			var topToolHeight = this.panelBodyTemplate.find(".buss-panel-top-toolbar").outerHeight(true)==null ? 0 :this.panelBodyTemplate.find(".buss-panel-top-toolbar").outerHeight(true);
			alert(topToolHeight);
			var bottomToolHeight = this.panelBodyTemplate.find(".buss-panel-bottom-toolbar").outerHeight(true) == null ? 0 :this.panelBodyTemplate.find(".buss-panel-bottom-toolbar").outerHeight(true);
			var _bodyHeight = this.element.outerHeight() - this.panelHeaderTemplate.outerHeight(true) - topToolHeight - bottomToolHeight;
			this.panelBodyTemplate.find(".buss-panel-body-content").html(this.bodyContent);
			this.panelBodyTemplate.find(".buss-panel-body-content").addClass(function(){
				return opts.bodyCls == null ? "" : opts.bodyCls;
			});
			var spacHeight = this.panelBodyTemplate.find(".buss-panel-body-content").outerHeight() - this.panelBodyTemplate.find(".buss-panel-body-content").height();
			this.panelBodyTemplate.find(".buss-panel-body-content").height(_bodyHeight - spacHeight - 3);
			
			if($.isFunction(opts.onOpen)){
				opts.onOpen.call(this.panelTemplate);
			}
		},
		setTitle:function(title){
			if (title != null){
				this.panelHeaderTemplate.find(".buss-panel-with-icon").html(title);
			}
		},
		setBodyHeight:function(height){
			var _bodyHeight = this.panelBodyTemplate.find(".buss-panel-body-content").height();
			var tmpHeight = height - _bodyHeight;
			alert(tmpHeight);
			this.panelBodyTemplate.find(".buss-panel-body-content").height(height);
			this.panelTemplate.height(this.panelTemplate.height() + tmpHeight);
		},
		loadPage:function(loadUrl,loadParams,iframe,onLoad){
			var opts = this.options;
			var _self = this;
			if($.trim(loadUrl) != ''){
				opts.loadUrl = loadUrl;
			}
			if($.trim(loadParams) != ''){
				opts.loadParams = loadParams;
			}
			if($.isFunction(onLoad)){
				opts.onLoad = onLoad;
			}
			if(iframe != undefined && iframe != null){
				opts.iframe = iframe;
			}
			if(opts.loadUrl == null || opts.loadUrl == ""){
				return;
			}			
			_self._load()
		},
		_load:function(){
			alert("ttt");
			var opts = this.options;		
			var _self = this;
			if (opts.iframe == false) {
				
				this.panelBodyTemplate.find(".buss-panel-body-content").load(opts.loadUrl, opts.loadParams, opts.onLoad);
			}else{
				var borderHeight = this.panelBodyTemplate.find(".buss-panel-body-content").outerHeight() - this.panelBodyTemplate.find(".buss-panel-body-content").height();
				var borderWidth = this.panelBodyTemplate.find(".buss-panel-body-content").outerWidth() - this.panelBodyTemplate.find(".buss-panel-body-content").width();
				var iframeVar = $("<iframe frameborder='no'></iframe>")
				iframeVar.height(this.panelBodyTemplate.find(".buss-panel-body-content").height() - borderHeight - 1);
				iframeVar.width(this.panelBodyTemplate.find(".buss-panel-body-content").width() - borderWidth - 1);
				iframeVar.attr("src",opts.loadUrl);
				iframeVar.load(opts.onLoad);
				this.panelBodyTemplate.find(".buss-panel-body-content").html(iframeVar);
			}
		},
		_setHeaderTool:function(){
			var opts = this.options;
			var _self = this;
			if(opts.header == false){
				this.panelHeaderTemplate.remove();
				return;
			}
			this.setTitle(opts.title);
			if(opts.closable == false){
				this.panelHeaderTemplate.find(".buss-panel-tool-close").remove();
			}else{
				this.panelHeaderTemplate.find(".buss-panel-tool-close").bind("click",function(){
					_self.destroy(_self);
				});
			}
			if(opts.minimizable == false){
				this.panelHeaderTemplate.find(".buss-panel-tool-min").remove();				
			}else{
				this.panelHeaderTemplate.find(".buss-panel-tool-min").bind("click",function(){
					_self.minimize(_self);
				})
			}
			if(opts.collapsible == false){
				this.panelHeaderTemplate.find(".buss-panel-tool-collapse").remove();
			}else{
				this.panelHeaderTemplate.find(".buss-panel-tool-collapse").bind("click",function(){
					if (opts.collapsibled == undefined || opts.collapsibled == false) {
						_self.collapsible();
					}else{
						_self.expand();
					}
				})
			}
			if (opts.iconCls == null){
				this.panelHeaderTemplate.find(".buss-panel-with-icon").css('padding-left','0');
			}else{
				this.panelHeaderTemplate.find(".buss-panel-icon").addClass(opts.iconCls);
			}
		},
		collapsible:function(target){
			var _self = target != null && target != undefined ? target : this;
			var opts = _self.options;
			if($.isFunction(opts.onBeforeCollapse)){
				if(opts.onBeforeCollapse.call(_self.panelTemplate) == false) return;
			}
			_self.panelTemplate.find(".buss-panel-body").hide();
			opts.collapsibled = true;
			if($.isFunction(opts.onCollapse)){
				opts.onCollapse.call(_self.panelTemplate);
			}
		},
		expand:function(target){
			var _self = target != null && target != undefined ? target : this;
			var opts = _self.options;
			if($.isFunction(opts.onBeforeExpand)){
				if(opts.onBeforeExpand.call(_self.panelTemplate) == false) return;
			}
			_self.panelTemplate.find(".buss-panel-body").show();
			opts.collapsibled = false;
			if($.isFunction(opts.onCollapse)){
				opts.onExpand.call(_self.panelTemplate);
			}
		},
		minimize:function(target){
			this._minimize(target);
		},
		_minimize:function(target){
			var _self = target != null && target != undefined ? target : this;
			var opts = _self.options;
			if(opts.minimized == true){
				return;
			}
			if($.isFunction(opts.onBeforeMinimize)){
				if(opts.onBeforeMinimize.call(_self.panelTemplate) == false) return;
			}
			_self.panelTemplate.hide();
			opts.minimized = true;
			if($.isFunction(opts.onMinimize)){
				opts.onMinimize.call(_self.panelTemplate);
			}
		},
		restore:function(target){			
			this._restore(target);
		},
		_restore:function(target){
			var _self = target != null && target != undefined ? target : this;
			var opts = _self.options;
			if(opts.minimized != true){
				return;
			}
			if($.isFunction(opts.onBeforeRestore)){
				if(opts.onBeforeRestore.call(_self.panelTemplate) == false) return;
			}
			_self.panelTemplate.show();
			opts.minimized = false;
			if($.isFunction(opts.onRestore)){
				opts.onRestore.call(_self.panelTemplate);
			}
		},
		close:function(target){			
			this._close(target);
		},
		_close:function(target){
			var _self = target != null && target != undefined ? target : this;
			var opts = _self.options;
			if($.isFunction(opts.onBeforeRestore)){
				if(opts.onBeforeClose.call(_self.panelTemplate) == false) return;
			}
			_self.panelTemplate.hide();
			opts.minimized = true;
			if($.isFunction(opts.onRestore)){
				opts.onClose.call(_self.panelTemplate);
			}
		},
		destroy:function(target){			
			this._destroy(target);
		},
		_destroy:function(target){
			var _self = target != null && target != undefined ? target : this;
			var opts = _self.options;
			if($.isFunction(opts.onBeforeDestroy)){
				if(opts.onBeforeDestroy.call(_self.panelTemplate) == false) return;
			}
			this.element.find("*").trigger("_destroy");
			_self.panelTemplate.unbind().remove();
			if($.isFunction(opts.onDestroy)){
				opts.onDestroy.call(_self.panelTemplate);
			}
		}
	})
})(jQuery)
